package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.CollectorListAdapter1;
import com.github.postsanf.yinian.adapter.CollectorListAdapter2;
import com.github.postsanf.yinian.adapter.CollectorListAdapter3;
import com.github.postsanf.yinian.adapter.CollectorListAdapter4;
import com.github.postsanf.yinian.bean.YNShopCar;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.widget.MySwipeMenuListView;
import com.github.postsanf.yinian.widget.SwipeMenu;
import com.github.postsanf.yinian.widget.SwipeMenuCreator;
import com.github.postsanf.yinian.widget.SwipeMenuItem;
import com.github.postsanf.yinian.widget.SwipeMenuListView;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ShopCarListActivity extends BaseActivity implements View.OnClickListener {
    private int userId;
    private List<YNShopCar> ynShopCarList1 = new ArrayList<>();
    private List<YNShopCar> ynShopCarList2 = new ArrayList<>();
    private List<YNShopCar> ynShopCarList3 = new ArrayList<>();
    private List<YNShopCar> ynShopCarList4 = new ArrayList<>();
    public static List<YNShopCar> ynShopCarClearAll = new ArrayList<>();
    public List<YNShopCar> ynShopCarModifyAll = new ArrayList<>();
    private MySwipeMenuListView mySwipeMenuListView1;
    private MySwipeMenuListView mySwipeMenuListView2;
    private MySwipeMenuListView mySwipeMenuListView3;
    private MySwipeMenuListView mySwipeMenuListView4;
    private CollectorListAdapter1 collectorListAdapter1;
    private CollectorListAdapter2 collectorListAdapter2;
    private CollectorListAdapter3 collectorListAdapter3;
    private CollectorListAdapter4 collectorListAdapter4;
    private LinearLayout ll_shopcar_empty;
    private RelativeLayout ll_voice, ll_lomo, ll_photo, ll_draw;
    private View ll_voice_kong, ll_lomo_kong, ll_photo_kong, ll_draw_kong;
    private ImageView sec_voice, sec_lomo, sec_photo, sec_draw, sec_all;
    private TextView heji, cash;
    private String hejiPrice;
    private RelativeLayout buttom;
    private String modifyInfoStr;
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    collectorListAdapter1.notifyDataSetChanged();
                    if (ynShopCarList1.size() > 0) {
                        ll_voice.setVisibility(View.VISIBLE);
                        ll_voice_kong.setVisibility(View.VISIBLE);
                    } else {
                        ll_voice.setVisibility(View.GONE);
                        ll_voice_kong.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    collectorListAdapter2.notifyDataSetChanged();
                    if (ynShopCarList2.size() > 0) {
                        ll_lomo.setVisibility(View.VISIBLE);
                        ll_lomo_kong.setVisibility(View.VISIBLE);
                    } else {
                        ll_lomo.setVisibility(View.GONE);
                        ll_lomo_kong.setVisibility(View.GONE);
                    }
                    break;
                case 3:
                    collectorListAdapter3.notifyDataSetChanged();
                    if (ynShopCarList3.size() > 0) {
                        ll_photo.setVisibility(View.VISIBLE);
                        ll_photo_kong.setVisibility(View.VISIBLE);
                    } else {
                        ll_photo.setVisibility(View.GONE);
                        ll_photo_kong.setVisibility(View.GONE);
                    }
                    break;
                case 4:
                    collectorListAdapter4.notifyDataSetChanged();
                    if (ynShopCarList4.size() > 0) {
                        ll_draw.setVisibility(View.VISIBLE);
                        ll_draw_kong.setVisibility(View.GONE);
                    } else {
                        ll_draw.setVisibility(View.GONE);
                        ll_draw_kong.setVisibility(View.GONE);
                    }
                    break;
                case 5:
                    if (ynShopCarList1.size() > 0 || ynShopCarList2.size() > 0 || ynShopCarList3.size() > 0 || ynShopCarList4.size() > 0) {
                        buttom.setVisibility(View.VISIBLE);
                        ll_shopcar_empty.setVisibility(View.GONE);
                    } else {
                        ll_shopcar_empty.setVisibility(View.VISIBLE);
                        buttom.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcarlist);
        initView();
        registerBoradcastReceiver();
        initShopCarList();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionUpdateHeJi);
        myIntentFilter.addAction(ACTIONs.actionAllSelect1);
        myIntentFilter.addAction(ACTIONs.actionAllSelect2);
        myIntentFilter.addAction(ACTIONs.actionAllSelect3);
        myIntentFilter.addAction(ACTIONs.actionAllSelect4);
        myIntentFilter.addAction(ACTIONs.actioncloseShopCarList);
        registerReceiver(mListReceiver, myIntentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
        ynShopCarClearAll.clear();
    }

    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionUpdateHeJi:
                    if (ynShopCarClearAll.size() > 0) {
                        if (ynShopCarClearAll.size() == 1) {
                            hejiPrice = ynShopCarClearAll.get(0).getNowPrice();
                            heji.setText("¥" + hejiPrice);
                            cash.setText("结算(" + 1 + ")");
                        } else {
                            BigDecimal totalInvestMoney = BigDecimal.ZERO;
                            for (int i = 0; i < ynShopCarClearAll.size(); i++) {
                                totalInvestMoney = totalInvestMoney.add(new BigDecimal(ynShopCarClearAll.get(i).getNowPrice()));
                            }
                            hejiPrice = totalInvestMoney.toString();
                            heji.setText("¥" + hejiPrice);
                            cash.setText("结算(" + ynShopCarClearAll.size() + ")");
                        }
                    } else {
                        hejiPrice = "0.0";
                        heji.setText("¥" + "0.0");
                        cash.setText("结算");
                    }
                    break;
                case ACTIONs.actionAllSelect1:
                    int alls1 = 1;
                    for (int i = 0; i < ynShopCarList1.size(); i++) {
                        if (ynShopCarList1.get(i).getSelect()) {
                            alls1 = alls1 * 1;
                        } else {
                            alls1 = alls1 * 0;
                        }
                    }
                    if (alls1 == 1) {
                        sec_voice.setImageResource(R.drawable.shopcar_select);
                    } else {
                        sec_voice.setImageResource(R.drawable.shopcar_unselect);
                    }
                    break;
                case ACTIONs.actionAllSelect2:
                    int alls2 = 1;
                    for (int i = 0; i < ynShopCarList2.size(); i++) {
                        if (ynShopCarList2.get(i).getSelect()) {
                            alls2 = alls2 * 1;
                        } else {
                            alls2 = alls2 * 0;
                        }
                    }
                    if (alls2 == 1) {
                        sec_lomo.setImageResource(R.drawable.shopcar_select);
                    } else {
                        sec_lomo.setImageResource(R.drawable.shopcar_unselect);
                    }
                    break;
                case ACTIONs.actionAllSelect3:
                    int alls3 = 1;
                    for (int i = 0; i < ynShopCarList3.size(); i++) {
                        if (ynShopCarList3.get(i).getSelect()) {
                            alls3 = alls3 * 1;
                        } else {
                            alls3 = alls3 * 0;
                        }
                    }
                    if (alls3 == 1) {
                        sec_photo.setImageResource(R.drawable.shopcar_select);
                    } else {
                        sec_photo.setImageResource(R.drawable.shopcar_unselect);
                    }
                    break;
                case ACTIONs.actionAllSelect4:
                    int alls4 = 1;
                    for (int i = 0; i < ynShopCarList4.size(); i++) {
                        if (ynShopCarList4.get(i).getSelect()) {
                            alls4 = alls4 * 1;
                        } else {
                            alls4 = alls4 * 0;
                        }
                    }
                    if (alls4 == 1) {
                        sec_draw.setImageResource(R.drawable.shopcar_select);
                    } else {
                        sec_draw.setImageResource(R.drawable.shopcar_unselect);
                    }
                    break;
                case ACTIONs.actioncloseShopCarList:
                    finish();
                    break;
            }
        }
    };

    private void initView() {
        userId = application.getCurID();
        findViewById(R.id.shopcar_back).setOnClickListener(this);
        ll_shopcar_empty = (LinearLayout) findViewById(R.id.ll_shopcar_empty);
        ll_voice = (RelativeLayout) findViewById(R.id.ll_voice);
        ll_voice_kong = (View) findViewById(R.id.ll_voice_kong);
        ll_lomo = (RelativeLayout) findViewById(R.id.ll_lomo);
        ll_lomo_kong = (View) findViewById(R.id.ll_lomo_kong);
        ll_photo = (RelativeLayout) findViewById(R.id.ll_photo);
        ll_photo_kong = (View) findViewById(R.id.ll_photo_kong);
        ll_draw = (RelativeLayout) findViewById(R.id.ll_draw);
        ll_draw_kong = (View) findViewById(R.id.ll_draw_kong);
        sec_voice = (ImageView) findViewById(R.id.sec_voice);
        sec_lomo = (ImageView) findViewById(R.id.sec_lomo);
        sec_photo = (ImageView) findViewById(R.id.sec_photo);
        sec_draw = (ImageView) findViewById(R.id.sec_draw);
        sec_all = (ImageView) findViewById(R.id.sec_all);
        heji = (TextView) findViewById(R.id.heji);
        cash = (TextView) findViewById(R.id.cash);
        buttom = (RelativeLayout) findViewById(R.id.buttom);
        sec_voice.setOnClickListener(this);
        sec_lomo.setOnClickListener(this);
        sec_photo.setOnClickListener(this);
        sec_draw.setOnClickListener(this);
        sec_all.setOnClickListener(this);
        cash.setOnClickListener(this);
        mySwipeMenuListView1 = (MySwipeMenuListView) findViewById(R.id.list1);
        mySwipeMenuListView2 = (MySwipeMenuListView) findViewById(R.id.list2);
        mySwipeMenuListView3 = (MySwipeMenuListView) findViewById(R.id.list3);
        mySwipeMenuListView4 = (MySwipeMenuListView) findViewById(R.id.list4);
        collectorListAdapter1 = new CollectorListAdapter1(ShopCarListActivity.this, ynShopCarList1);
        collectorListAdapter2 = new CollectorListAdapter2(ShopCarListActivity.this, ynShopCarList2);
        collectorListAdapter3 = new CollectorListAdapter3(ShopCarListActivity.this, ynShopCarList3);
        collectorListAdapter4 = new CollectorListAdapter4(ShopCarListActivity.this, ynShopCarList4);
        mySwipeMenuListView1.setAdapter(collectorListAdapter1);
        mySwipeMenuListView2.setAdapter(collectorListAdapter2);
        mySwipeMenuListView3.setAdapter(collectorListAdapter3);
        mySwipeMenuListView4.setAdapter(collectorListAdapter4);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
                openItem.setBackground(new ColorDrawable(Color.rgb(250, 50,
                        50)));
                openItem.setWidth(UIHelper.dip2px(ShopCarListActivity.this, 90));
                openItem.setTitle("删除");
                openItem.setTitleSize(16);
                openItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(openItem);
            }
        };
        mySwipeMenuListView1.setMenuCreator(creator);
        mySwipeMenuListView2.setMenuCreator(creator);
        mySwipeMenuListView3.setMenuCreator(creator);
        mySwipeMenuListView4.setMenuCreator(creator);
        mySwipeMenuListView1.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                RemoveYnShopCar(ynShopCarList1.get(position), position, 1);
                return false;
            }
        });
        mySwipeMenuListView2.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                RemoveYnShopCar(ynShopCarList2.get(position), position, 2);
                return false;
            }
        });
        mySwipeMenuListView3.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                RemoveYnShopCar(ynShopCarList3.get(position), position, 3);
                return false;
            }
        });
        mySwipeMenuListView4.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                RemoveYnShopCar(ynShopCarList4.get(position), position, 4);
                return false;
            }
        });
    }

    boolean bbVoice = false;
    boolean bbLomo = false;
    boolean bbPhoto = false;
    boolean bbDraw = false;
    boolean bbAll = false;

    private void secVoiceSelect() {
        sec_voice.setImageResource(R.drawable.shopcar_select);
        for (int i = 0; i < ynShopCarList1.size(); i++) {
            if (ynShopCarList1.get(i).getSelect()) {
                ynShopCarClearAll.remove(ynShopCarList1.get(i));
            }
            ynShopCarClearAll.add(ynShopCarList1.get(i));
            ynShopCarList1.get(i).setSelect(true);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter1.notifyDataSetChanged();
    }

    private void secVoiceUnSelect() {
        sec_voice.setImageResource(R.drawable.shopcar_unselect);
        for (int i = 0; i < ynShopCarList1.size(); i++) {
            ynShopCarClearAll.remove(ynShopCarList1.get(i));
            ynShopCarList1.get(i).setSelect(false);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter1.notifyDataSetChanged();
    }

    private void secLomoSelect() {
        sec_lomo.setImageResource(R.drawable.shopcar_select);
        for (int i = 0; i < ynShopCarList2.size(); i++) {
            if (ynShopCarList2.get(i).getSelect()) {
                ynShopCarClearAll.remove(ynShopCarList2.get(i));
            }
            ynShopCarClearAll.add(ynShopCarList2.get(i));
            ynShopCarList2.get(i).setSelect(true);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter2.notifyDataSetChanged();
    }

    private void secLomoUnSelect() {
        sec_lomo.setImageResource(R.drawable.shopcar_unselect);
        for (int i = 0; i < ynShopCarList2.size(); i++) {
            ynShopCarClearAll.remove(ynShopCarList2.get(i));
            ynShopCarList2.get(i).setSelect(false);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter2.notifyDataSetChanged();
    }

    private void secPhotoSelect() {
        sec_photo.setImageResource(R.drawable.shopcar_select);
        for (int i = 0; i < ynShopCarList3.size(); i++) {
            if (ynShopCarList3.get(i).getSelect()) {
                ynShopCarClearAll.remove(ynShopCarList3.get(i));
            }
            ynShopCarClearAll.add(ynShopCarList3.get(i));
            ynShopCarList3.get(i).setSelect(true);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter3.notifyDataSetChanged();
    }

    private void secPhotoUnSelect() {
        sec_photo.setImageResource(R.drawable.shopcar_unselect);
        for (int i = 0; i < ynShopCarList3.size(); i++) {
            ynShopCarClearAll.remove(ynShopCarList3.get(i));
            ynShopCarList3.get(i).setSelect(false);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter3.notifyDataSetChanged();
    }

    private void secDrawSelect() {
        sec_draw.setImageResource(R.drawable.shopcar_select);
        for (int i = 0; i < ynShopCarList4.size(); i++) {
            if (ynShopCarList4.get(i).getSelect()) {
                ynShopCarClearAll.remove(ynShopCarList4.get(i));
            }
            ynShopCarClearAll.add(ynShopCarList4.get(i));
            ynShopCarList4.get(i).setSelect(true);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter4.notifyDataSetChanged();
    }

    private void secDrawUnSelect() {
        sec_draw.setImageResource(R.drawable.shopcar_unselect);
        for (int i = 0; i < ynShopCarList4.size(); i++) {
            ynShopCarClearAll.remove(ynShopCarList4.get(i));
            ynShopCarList4.get(i).setSelect(false);
        }
        sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
        collectorListAdapter4.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shopcar_back:
                ModifyYnShopCar();
                finish();
                break;
            case R.id.sec_voice:
                if (!bbVoice) {
                    bbVoice = true;
                    secVoiceSelect();
                } else {
                    bbVoice = false;
                    secVoiceUnSelect();
                }
                break;
            case R.id.sec_lomo:
                if (!bbLomo) {
                    bbLomo = true;
                    secLomoSelect();
                } else {
                    bbLomo = false;
                    secLomoUnSelect();
                }
                break;
            case R.id.sec_photo:
                if (!bbPhoto) {
                    bbPhoto = true;
                    secPhotoSelect();
                } else {
                    bbPhoto = false;
                    secPhotoUnSelect();
                }
                break;
            case R.id.sec_draw:
                if (!bbDraw) {
                    bbDraw = true;
                    secDrawSelect();
                } else {
                    bbDraw = false;
                    secDrawUnSelect();
                }
                break;
            case R.id.sec_all:
                if (!bbAll) {
                    bbAll = true;
                    sec_all.setImageResource(R.drawable.shopcar_select);
                    secVoiceSelect();
                    secLomoSelect();
                    secPhotoSelect();
                    secDrawSelect();
                } else {
                    bbAll = false;
                    sec_all.setImageResource(R.drawable.shopcar_unselect);
                    secVoiceUnSelect();
                    secLomoUnSelect();
                    secPhotoUnSelect();
                    secDrawUnSelect();
                }
                break;
            case R.id.cash:
                if (ynShopCarClearAll.size() > 0) {
                    startActivity(new Intent(ShopCarListActivity.this, ShopCarOrderActivity.class).putExtra("hejiPrice", hejiPrice));
                    ModifyYnShopCar();
                } else {
                    showToast("结算前请勾选物品");
                }
                break;
        }
    }

    private void initShopCarList() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynShowCart))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        Log.i("xjs", "shops===>" + s.toString());
                        try {
                            JSONObject jsonObject = new JSONObject(s.toString());
                            if (jsonObject.optInt("code") == 0) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject goods = jsonArray.optJSONObject(i);
                                    JSONArray goodsInfo = goods.optJSONArray("goodsInfo");
                                    String goodsName = goods.optString("goodsName");
                                    if (goodsName.equals("声音明信片")) {
                                        for (int j = 0; j < goodsInfo.length(); j++) {
                                            JSONObject eb = goodsInfo.optJSONObject(j);
                                            YNShopCar ynShopCar = new YNShopCar();
                                            ynShopCar.setEbGoodsName(eb.optString("ebGoodsName"));
                                            ynShopCar.setEbGoodsPic(eb.optString("ebGoodsPic"));
                                            ynShopCar.setCartItemNum(eb.optInt("cartItemNum"));
                                            ynShopCar.setEbGoodsEnName(eb.optString("ebGoodsEnName"));
                                            ynShopCar.setEbGoodsID(eb.optInt("ebGoodsID"));
                                            ynShopCar.setCartID(eb.optInt("cartID"));
                                            ynShopCar.setEbGoodsUnit(eb.optString("ebGoodsUnit"));
                                            ynShopCar.setEbGoodsPrice(eb.optString("ebGoodsPrice"));
                                            ynShopCar.setCartItemID(eb.optInt("cartItemID"));
                                            ynShopCarList1.add(ynShopCar);
                                        }
                                    } else if (goodsName.equals("LOMO卡")) {
                                        for (int j = 0; j < goodsInfo.length(); j++) {
                                            JSONObject eb = goodsInfo.optJSONObject(j);
                                            YNShopCar ynShopCar = new YNShopCar();
                                            ynShopCar.setEbGoodsName(eb.optString("ebGoodsName"));
                                            ynShopCar.setEbGoodsPic(eb.optString("ebGoodsPic"));
                                            ynShopCar.setCartItemNum(eb.optInt("cartItemNum"));
                                            ynShopCar.setEbGoodsEnName(eb.optString("ebGoodsEnName"));
                                            ynShopCar.setEbGoodsID(eb.optInt("ebGoodsID"));
                                            ynShopCar.setCartID(eb.optInt("cartID"));
                                            ynShopCar.setEbGoodsUnit(eb.optString("ebGoodsUnit"));
                                            ynShopCar.setEbGoodsPrice(eb.optString("ebGoodsPrice"));
                                            ynShopCar.setCartItemID(eb.optInt("cartItemID"));
                                            ynShopCarList2.add(ynShopCar);
                                        }
                                    } else if (goodsName.equals("照片拼图")) {
                                        for (int j = 0; j < goodsInfo.length(); j++) {
                                            JSONObject eb = goodsInfo.optJSONObject(j);
                                            YNShopCar ynShopCar = new YNShopCar();
                                            ynShopCar.setEbGoodsName(eb.optString("ebGoodsName"));
                                            ynShopCar.setEbGoodsPic(eb.optString("ebGoodsPic"));
                                            ynShopCar.setCartItemNum(eb.optInt("cartItemNum"));
                                            ynShopCar.setEbGoodsEnName(eb.optString("ebGoodsEnName"));
                                            ynShopCar.setEbGoodsID(eb.optInt("ebGoodsID"));
                                            ynShopCar.setCartID(eb.optInt("cartID"));
                                            ynShopCar.setEbGoodsUnit(eb.optString("ebGoodsUnit"));
                                            ynShopCar.setEbGoodsPrice(eb.optString("ebGoodsPrice"));
                                            ynShopCar.setCartItemID(eb.optInt("cartItemID"));
                                            ynShopCarList3.add(ynShopCar);
                                        }
                                    } else if (goodsName.equals("图钉相纸")) {
                                        for (int j = 0; j < goodsInfo.length(); j++) {
                                            JSONObject eb = goodsInfo.optJSONObject(j);
                                            YNShopCar ynShopCar = new YNShopCar();
                                            ynShopCar.setEbGoodsName(eb.optString("ebGoodsName"));
                                            ynShopCar.setEbGoodsPic(eb.optString("ebGoodsPic"));
                                            ynShopCar.setCartItemNum(eb.optInt("cartItemNum"));
                                            ynShopCar.setEbGoodsEnName(eb.optString("ebGoodsEnName"));
                                            ynShopCar.setEbGoodsID(eb.optInt("ebGoodsID"));
                                            ynShopCar.setCartID(eb.optInt("cartID"));
                                            ynShopCar.setEbGoodsUnit(eb.optString("ebGoodsUnit"));
                                            ynShopCar.setEbGoodsPrice(eb.optString("ebGoodsPrice"));
                                            ynShopCar.setCartItemID(eb.optInt("cartItemID"));
                                            ynShopCarList4.add(ynShopCar);
                                        }
                                    }
                                }
                                myHandler.sendEmptyMessage(1);
                                myHandler.sendEmptyMessage(2);
                                myHandler.sendEmptyMessage(3);
                                myHandler.sendEmptyMessage(4);
                                myHandler.sendEmptyMessage(5);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    private void RemoveYnShopCar(final YNShopCar ynShopCar, final int position, final int type) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        mParam.put("itemID", String.valueOf(ynShopCar.getCartItemID()));
        mParam.put("cartID", String.valueOf(ynShopCar.getCartID()));
        mParam.put("type", "remove");
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynManageCart))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Toast.makeText(ShopCarListActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                        switch (type) {
                            case 1:
                                if (ynShopCar.getSelect()) {
                                    ynShopCarClearAll.remove(ynShopCar);
                                    sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                                }
                                ynShopCarList1.remove(position);
                                myHandler.sendEmptyMessage(1);
                                break;
                            case 2:
                                if (ynShopCar.getSelect()) {
                                    ynShopCarClearAll.remove(ynShopCar);
                                    sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                                }
                                ynShopCarList2.remove(position);
                                myHandler.sendEmptyMessage(2);
                                break;
                            case 3:
                                if (ynShopCar.getSelect()) {
                                    ynShopCarClearAll.remove(ynShopCar);
                                    sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                                }
                                ynShopCarList3.remove(position);
                                myHandler.sendEmptyMessage(3);
                                break;
                            case 4:
                                if (ynShopCar.getSelect()) {
                                    ynShopCarClearAll.remove(ynShopCar);
                                    sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                                }
                                ynShopCarList4.remove(position);
                                myHandler.sendEmptyMessage(4);
                                break;
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ModifyYnShopCar();
    }


    private void ModifyYnShopCar() {
        ynShopCarModifyAll.addAll(ynShopCarList1);
        ynShopCarModifyAll.addAll(ynShopCarList2);
        ynShopCarModifyAll.addAll(ynShopCarList3);
        ynShopCarModifyAll.addAll(ynShopCarList4);
        try {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < ynShopCarModifyAll.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("number", ynShopCarModifyAll.get(i).getCartItemNum());
                jsonObject.put("cartID", ynShopCarModifyAll.get(i).getCartID());
                jsonArray.put(jsonObject);
            }
            modifyInfoStr = jsonArray.toString();
            LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
            mParam.put("userid", String.valueOf(application.getCurID()));
            mParam.put("modifyInfo", modifyInfoStr);
            mParam.put("type", "modify");
            StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynManageCart))
                    .setMethod(HttpMethods.Post)
                    .setParamMap(mParam)
                    .setHttpListener(new HttpListener<String>(true, false, true) {
                        @Override
                        public void onSuccess(String s, Response<String> response) {
                            Log.i("xjs", "modify==>" + s);

                        }

                        @Override
                        public void onFailure(HttpException e, Response<String> response) {
                            showToast(CommonConstants.YN_NET_LINK);
                        }
                    });
            liteHttp.executeAsync(req);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
